package com.fram.controller.api.android;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fram.bean.admin.android.order.request.OrderAndroidRequest;
import com.fram.bean.admin.android.order.request.OrderDeliveredAndroidRequest;
import com.fram.bean.admin.android.order.request.ReportFormRequest;
import com.fram.bean.sys.PageBean;
import com.fram.bean.sys.ResponseMsg;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProException;
import com.fram.common.util.PublicMethod;
import com.fram.common.util.apiversion.ApiVersion;
import com.fram.common.util.base.BaseController;
import com.fram.service.api.v1.android.OrderAndroidService;

@RestController
@RequestMapping("/api/{version}/app/manage/order")
public class OrderAndroidController extends BaseController{
	
	private static Logger LOG = LoggerFactory.getLogger(OrderAndroidController.class);

	@Autowired
	private OrderAndroidService orderAndroidService;
	
	@ApiVersion(1.0)
	@RequestMapping("list")
	private ResponseMsg list(@RequestBody PageBean<OrderAndroidRequest> request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			request.getParam().setDistributorId(getDistributorId());
			msg.setData(orderAndroidService.list(request));
		} catch (ProException e) {
			LOG.error("-首页接单-配送出错-->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("--首页接单-配送异常--->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg; 
	}
	
	@ApiVersion(1.0)
	@RequestMapping("detail")
	private ResponseMsg detail(Long id) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(orderAndroidService.detail(id));
		} catch (ProException e) {
			LOG.error("-首页接单-配送详情出错-->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("--首页接单-配送详情异常--->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	
	@ApiVersion(1.0)
	@RequestMapping("orderDelivered")
	private ResponseMsg orderDelivered(@RequestBody OrderDeliveredAndroidRequest request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			orderAndroidService.orderDelivered(request);
		} catch (ProException e) {
			LOG.error("-首页接单-配送详情出错-->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("--首页接单-配送详情异常--->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	
	@ApiVersion(1.0)
	@RequestMapping("salesVolume")
	private ResponseMsg salesVolume() {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(orderAndroidService.salesVolume(getDistributorId()));
		} catch (ProException e) {
			LOG.error("-销售额出错-->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("--销售额异常--->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	
	//报表
	@ApiVersion(1.0)
	@RequestMapping("reportform")
	private ResponseMsg reportForm(@RequestBody ReportFormRequest request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			request.setDistributorId(getDistributorId());
			msg.setData(orderAndroidService.reportForm(request));
		} catch (ProException e) {
			LOG.error("-报表出错-->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("--报表异常--->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	
	//店铺 删除订单 需要加字段
	@ApiVersion(1.0)
	@RequestMapping("delete")
	private ResponseMsg delete() {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		
		return msg;
	}
	
}
